/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.gnarly;

import java.util.Hashtable;
import java.util.Map;

/**
 * FloodProtection.java
 * 
 * A hashtable to hold all current connections that are active to filter out flood attacks
 * 
 * @author gnarly
 * @author graham
 */
public class FloodProtection {

	public static Map<String, Integer> hostlist = new Hashtable<String, Integer>();

	public static void add(String remoteAddress) {
		Integer ct = hostlist.get(remoteAddress);
		if(ct == null) {
			ct = 1;
		} else {
			ct++;
		}
		hostlist.put(remoteAddress,ct);
	}

	public static void remove(String remoteAddress) {
		if(hostlist.containsKey(remoteAddress)) {
			int ct = hostlist.get(remoteAddress);
			ct--;
			if(ct == 0) {
				hostlist.remove(remoteAddress);
			} else {
				hostlist.put(remoteAddress,ct);
			}
		}
	}

	public static boolean has(String remoteAddress, int checkCount) {
		Integer count = hostlist.get(remoteAddress);
		if(count == null)
			return false;
		if(count >= checkCount)
			return true;
		return false;
	}
	
}